package hbase;

import dao.Student;
import javafx.scene.control.Tab;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.ipc.Server;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;




//该处为 hbase 连接数据库
public class HbaseUtil {
    static final String c = "cf1";

    public String insertinfo(String ip, String port, String tableName, List<Student> list) throws Exception {
        Connection con = getConnection(ip, port);
        HBaseAdmin admin = (HBaseAdmin) con.getAdmin();
        Table table = con.getTable(TableName.valueOf(tableName));
        boolean m = admin.tableExists(TableName.valueOf(tableName));
        if (!m) {
            createTable(admin, tableName);
        }
        insertData(table, list);
        System.out.println("表中的数据列！");

        return "添加成功！";
    }

    //添加判断是否存在方法（惠文）
    public String updateinfo(String ip, String port, String id, List<Student> list) throws Exception {
        Connection con = getConnection(ip, port);
        HBaseAdmin admin = (HBaseAdmin) con.getAdmin();
        Table table = con.getTable(TableName.valueOf("student"));
        updateData(table, list);
        Get get = new Get(id.getBytes());
        String m="修改成功！";
        String n="修改失败！";
        if ((get.addColumn(c.getBytes(), "name".getBytes()) != null)) {
            System.out.println("修改成功！");
            return  m;
        }
        else
            return  n;

    }
//判断是否存在（浩鑫）
    public String changeinfo(String ip, String port, String id, String name,List<Student> list) throws Exception {
        Connection con = getConnection(ip, port);
        HBaseAdmin admin = (HBaseAdmin) con.getAdmin();
        Table table = con.getTable(TableName.valueOf("student"));
        ChangeStatus(table, list);
        Get get = new Get(id.getBytes());
        Get get1 = new Get(name.getBytes());
        if ((get.addColumn(c.getBytes(), "number".getBytes()) != null )||(get1.addColumn(c.getBytes(),"number".getBytes())!= null)) {

            System.out.println("修改成功！");
            return "修改成功！";
        }
        else
            return "修改失败！";

    }

//    public String changeinfo1(String ip, String port, String name, List<Student> list) throws Exception {
//        Connection con = getConnection(ip, port);
//        HBaseAdmin admin = (HBaseAdmin) con.getAdmin();
//        Table table = con.getTable(TableName.valueOf("student"));
//        ChangeStatus1(table, list);
//       // Get get = new Get(id.getBytes());
//        Get get1 = new Get(name.getBytes());
//        if ( (get1.addColumn(c.getBytes(),"id".getBytes())!= null)) {
//            //   (get.addColumn(c.getBytes(), "name".getBytes()) != null )
//            System.out.println("修改成功！");
//            return "修改成功！";
//        }
//        else
//            return "修改失败！";
//
//    }

    private void createTable(Admin admin,String tableName) throws Exception {
        HTableDescriptor desc1 = new HTableDescriptor(TableName.valueOf(tableName));
        HColumnDescriptor desc2 = new HColumnDescriptor("info");
        desc1.addFamily(desc2);
        admin.createTable(desc1);
    }
    //        插入数据的方法
    private void insertData(Table table,List<Student> list) throws Exception {
        for (Student student : list) {
            Put put = new Put(student.getId().getBytes());
            put.addColumn(c.getBytes(),"name".getBytes(),student.getName().getBytes());
           // put.addColumn(c.getBytes(),"date".getBytes(),student.getDate().getBytes());
            put.addColumn(c.getBytes(),"number".getBytes(),student.getNumber().getBytes());
            put.addColumn(c.getBytes(),"semester".getBytes(),student.getSemester().getBytes());
            put.addColumn(c.getBytes(),"status".getBytes(),student.getStatus().getBytes());
            table.put(put);
        }
    }
    //更新数据的方法（惠文）
    private void updateData(Table table,List<Student> list) throws Exception {
        for (Student student : list) {
            Put put = new Put(student.getId().getBytes());
            put.addColumn(c.getBytes(),"name".getBytes(),student.getName().getBytes());
            put.addColumn(c.getBytes(),"number".getBytes(),student.getNumber().getBytes());
            put.addColumn(c.getBytes(),"status".getBytes(),student.getStatus().getBytes());
            table.put(put);
        }
    }

    //更新数据的方法（浩鑫）
    private void ChangeStatus(Table table,List<Student> list) throws Exception {
        for (Student student : list) {
            Put put = new Put(student.getId().getBytes());
            Put put1 = new Put(student.getName().getBytes());
            put.addColumn(c.getBytes(),"status".getBytes(),student.getStatus().getBytes());
           put1.addColumn(c.getBytes(),"status".getBytes(),student.getStatus().getBytes());
            table.put(put);
            table.put(put1);
        }
    }
//    private void ChangeStatus1(Table table,List<Student> list) throws Exception {
//        for (Student student : list) {
//           // Put put = new Put(student.getId().getBytes());
//            Put put1 = new Put(student.getName().getBytes());
//            //put.addColumn(c.getBytes(),"status".getBytes(),student.getStatus().getBytes());
//             put1.addColumn(c.getBytes(),"status".getBytes(),student.getStatus().getBytes());
//           // table.put(put);
//              table.put(put1);
//        }
//    }



    //    获得与 hbase 的连接
    private static Connection getConnection(String ip,String port) throws Exception {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum",ip);
        configuration.set("hbase.zookeeper.property.clientPort",port);
        Connection connection = ConnectionFactory.createConnection(configuration);
        return connection;

    }



}
